﻿/******************************* 
** 作者： 斯泰尔 
** 时间： 2021-07-26 16:17:35 
** 版本: V1.0.0 
** CLR: 4.0.30319.42000 
** GUID: 8ccbaed6-700a-4c6b-93bc-d257f6fd51a8 
** 描述： 尚未编写描述 
*******************************/
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Data;

namespace AliSMS
{
    class InitFile
    {
        SQLiteHelper sqlite = new SQLiteHelper();

        public string accessKeyId = ConfigurationManager.AppSettings["accessKeyId"];
        public string accessKeySecret = ConfigurationManager.AppSettings["accessKeySecret"];
        public string SignName = ConfigurationManager.AppSettings["SignName"];

       public int sendtime = int.Parse(ConfigurationManager.AppSettings["SendTime"]);
       public bool IsAuto = bool.Parse(ConfigurationManager.AppSettings["IsAuto"]);

       #region 使DataGridView的列自适应宽度
       /// <summary>
       /// 使DataGridView的列自适应宽度
       /// </summary>
       /// <param name="dgViewFiles"></param>
       public void AutoSizeColumn(System.Windows.Forms.DataGridView dgViewFiles)
       {
           int width = 0;
           dgViewFiles.ColumnHeadersDefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
           //使列自使用宽度
           //对于DataGridView的每一个列都调整
           for (int i = 0; i < dgViewFiles.Columns.Count; i++)
           {
               //将每一列都调整为自动适应模式
               dgViewFiles.AutoResizeColumn(i, System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells);
               //记录整个DataGridView的宽度
               width += dgViewFiles.Columns[i].Width;
           }
           //判断调整后的宽度与原来设定的宽度的关系，如果是调整后的宽度大于原来设定的宽度，
           //则将DataGridView的列自动调整模式设置为显示的列即可，
           //如果是小于原来设定的宽度，将模式改为填充。
           if (width > dgViewFiles.Size.Width)
           {
               dgViewFiles.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.DisplayedCells;
           }
           else
           {
               dgViewFiles.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
           }
       }
       #endregion

       #region 使DataGridView的列表头中文转换
        /// <summary>
       /// 使DataGridView的列表头中文转换
        /// </summary>
       /// <param name="dGV">DataGridView</param>
        /// <param name="NoDispFld">不转换字段</param>
       public void DataViewColumn(System.Windows.Forms.DataGridView dGV, string NoDispFld)
       {
           try
           {
               DataTable dt = sqlite.ExecuteDt("select fdname,chnname from fld", @"Data Source=Data.db");
               Dictionary<string, string> dict = dt.Rows.Cast<DataRow>().ToDictionary(x => x[0].ToString(), x => x[1].ToString());

               for (int i = 0; i < dGV.Columns.Count; i++)
               {
                   string colName = dGV.Columns[i].Name.ToString().ToLower().Trim();
                   dGV.Columns[i].Name = colName;
                   dGV.Columns[i].ToolTipText = "字段名：" + colName;
                   dGV.Columns[i].HeaderText = colName == "sel" ? dGV.Columns[i].HeaderText : dict.ContainsKey(colName) ? dict[colName] : colName; ;
                   if (colName == "id" || colName == "totalnum")
                   {
                       dGV.Columns[i].Visible = false;
                   }
               }
           }
           catch (Exception ex)
           {
               WriteLog(ex.ToString(), true);
           }
       }
       #endregion

       #region 日志
       public void WriteLog(string msg, bool isdebug)
       {
           string text = AppDomain.CurrentDomain.BaseDirectory;
           text = Path.Combine(text, "log");
           if (!Directory.Exists(text))
           {
               Directory.CreateDirectory(text);
           }
           if (isdebug)
           {
               text = Path.Combine(text, "debug");
               if (!Directory.Exists(text))
               {
                   Directory.CreateDirectory(text);
               }
           }
           text = Path.Combine(text, DateTime.Now.ToString("yyyyMMdd") + ".txt");
           if (!File.Exists(text))
           {
               File.CreateText(text).Close();
           }
           StreamWriter streamWriter = new StreamWriter(text, true, Encoding.UTF8);
           if (isdebug)
           {
               streamWriter.WriteLine("--------------------------------------------------------------------------------------------------------------------------------");
           }
           streamWriter.WriteLine("{0}  {1}", DateTime.Now.ToString("yyyyMMdd HH:mm:ss"), msg);
           if (isdebug)
           {
               streamWriter.WriteLine("--------------------------------------------------------------------------------------------------------------------------------");
           }
           streamWriter.Flush();
           streamWriter.Close();
       }

       public void ClearLog()
       {
           string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log");
           if (Directory.Exists(path))
           {
               DirectoryInfo directoryInfo = new DirectoryInfo(path);
               FileInfo[] files = directoryInfo.GetFiles("*.*", SearchOption.AllDirectories);
               FileInfo[] array = files;
               for (int i = 0; i < array.Length; i++)
               {
                   FileInfo fileInfo = array[i];
                   if (DateTime.Now.Subtract(fileInfo.CreationTime).Days > 30)
                   {
                       File.Delete(fileInfo.FullName);
                   }
               }
           }
       }
       #endregion
    }
}
